package com.bai.edu.service.impl;

import com.bai.edu.client.VodClient;
import com.bai.edu.entity.Video;
import org.springframework.util.StringUtils;
import com.bai.edu.mapper.VideoMapper;
import com.bai.edu.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author bai
 * @since 2023-07-25
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public void removeVideoByCourseId(String courseId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.select("video_source_id");
        List<Video> videos = baseMapper.selectList(queryWrapper);
        List<String> list = new ArrayList<>();
        for (Video video : videos) {
            String sourceId = video.getVideoSourceId();
            if (!StringUtils.isEmpty(sourceId)) {
                list.add(sourceId);
            }
        }
        if (list.size() > 0) {
            vodClient.deleteBatch(list);
        }
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        baseMapper.delete(videoQueryWrapper);
    }
}
